{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Refine 精炼\n",
    "\n",
    "原文: https://docs.sympy.org/latest/modules/assumptions/refine.html\n",
    "\n",
    "## 方法 sympy.assumptions.refine.refine(expr, assumptions=True)\n",
    "\n",
    "使用假设简化表达式。\n",
    "\n",
    "### 解释\n",
    "\n",
    "不像 `simplify()` 没有任何假设就进行结构化简，此函数将表达式转换成只有在某些假设下才有效的形式。注意，`simplify()`一般不在`refining`过程中完成。\n",
    "\n",
    "`Refining`布尔表达式涉及到将其简化为 `S.true` 或 `S.false`。与 `ask()`不同，如果不能确定真值，表达式不会减少。\n",
    "\n",
    "### 例子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left|{x}\\right|$"
      ],
      "text/plain": [
       "Abs(x)"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sympy import refine, sqrt, Q\n",
    "from sympy.abc import x\n",
    "refine(sqrt(x**2), Q.real(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle x$"
      ],
      "text/plain": [
       "x"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "refine(sqrt(x**2), Q.positive(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\text{True}$"
      ],
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "refine(Q.real(x), Q.positive(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle Q.positive(x)$"
      ],
      "text/plain": [
       "Q.positive(x)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "refine(Q.positive(x), Q.real(x))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 参见\n",
    "> - sympy.simplify.simplify.simplify 没有假设的结构简化。\n",
    "> - sympy.assumptions.ask.ask 使用假设查询布尔表达式。\n",
    "\n",
    "## 方法 sympy.assumptions.refine.refine_Pow(expr, assumptions)\n",
    "\n",
    "指数精炼处理。\n",
    "\n",
    "#### 例子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n",
      "1\n",
      "-1\n",
      "(-1)**y\n",
      "(-1)**y\n",
      "(-1)**(y + 1)\n",
      "(-1)**(x + 1)\n"
     ]
    }
   ],
   "source": [
    "from sympy import Q\n",
    "from sympy.assumptions.refine import refine_Pow\n",
    "from sympy.abc import x,y,z\n",
    "print(refine_Pow((-1)**x, Q.real(x)))\n",
    "print(refine_Pow((-1)**x, Q.even(x)))\n",
    "print(refine_Pow((-1)**x, Q.odd(x)))\n",
    "# 对于 -1的幂，甚至指数的某些部分也可以简化:\n",
    "print(refine_Pow((-1)**(x+y), Q.even(x)))\n",
    "print(refine_Pow((-1)**(x+y+z), Q.odd(x) & Q.odd(z)))\n",
    "print(refine_Pow((-1)**(x+y+2), Q.odd(x)))\n",
    "print(refine_Pow((-1)**(x+3), True))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 方法 sympy.assumptions.refine.refine_abs(expr, assumptions)\n",
    "\n",
    "绝对值的精炼处理。\n",
    "\n",
    "### 例子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n",
      "x\n",
      "-x\n"
     ]
    }
   ],
   "source": [
    "from sympy import Q, Abs\n",
    "from sympy.assumptions.refine import refine_abs\n",
    "from sympy.abc import x\n",
    "print(refine_abs(Abs(x), Q.real(x)))\n",
    "print(refine_abs(Abs(x), Q.positive(x)))\n",
    "print(refine_abs(Abs(x), Q.negative(x)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 方法 sympy.assumptions.refine.refine_arg(expr, assumptions)\n",
    "\n",
    "复杂参数的精炼处理\n",
    "\n",
    "### 解释"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "pi\n"
     ]
    }
   ],
   "source": [
    "from sympy.assumptions.refine import refine_arg\n",
    "from sympy import Q, arg\n",
    "from sympy.abc import x\n",
    "print(refine_arg(arg(x), Q.positive(x)))\n",
    "print(refine_arg(arg(x), Q.negative(x)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 方法 sympy.assumptions.refine.refine_atan2(expr, assumptions)\n",
    "\n",
    "atan2函数的精炼处理。\n",
    "\n",
    "### 例子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "atan(y/x)\n",
      "atan(y/x) - pi\n",
      "atan(y/x) + pi\n",
      "pi\n",
      "pi/2\n",
      "-pi/2\n",
      "nan\n"
     ]
    }
   ],
   "source": [
    "from sympy import Q, atan2\n",
    "from sympy.assumptions.refine import refine_atan2\n",
    "from sympy.abc import x, y\n",
    "print(refine_atan2(atan2(y,x), Q.real(y) & Q.positive(x)))\n",
    "print(refine_atan2(atan2(y,x), Q.negative(y) & Q.negative(x)))\n",
    "print(refine_atan2(atan2(y,x), Q.positive(y) & Q.negative(x)))\n",
    "print(refine_atan2(atan2(y,x), Q.zero(y) & Q.negative(x)))\n",
    "print(refine_atan2(atan2(y,x), Q.positive(y) & Q.zero(x)))\n",
    "print(refine_atan2(atan2(y,x), Q.negative(y) & Q.zero(x)))\n",
    "print(refine_atan2(atan2(y,x), Q.zero(y) & Q.zero(x)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 方法 sympy.assumptions.refine.refine_im(expr, assumptions)\n",
    "\n",
    "虚部的精炼处理。\n",
    "\n",
    "### 解释"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "-I*x\n"
     ]
    }
   ],
   "source": [
    "from sympy.assumptions.refine import refine_im\n",
    "from sympy import Q, im\n",
    "from sympy.abc import x\n",
    "print(refine_im(im(x), Q.real(x)))\n",
    "print(refine_im(im(x), Q.imaginary(x)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## sympy.assumptions.refine.refine_matrixelement(expr, assumptions)\n",
    "\n",
    "对称部分的精炼处理程序。\n",
    "\n",
    "### 例子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle X_{0, 1}$"
      ],
      "text/plain": [
       "X[0, 1]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sympy.assumptions.refine import refine_matrixelement\n",
    "from sympy import Q\n",
    "from sympy.matrices.expressions.matexpr import MatrixSymbol\n",
    "X = MatrixSymbol('X', 3, 3)\n",
    "refine_matrixelement(X[0, 1], Q.symmetric(X))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle X_{0, 1}$"
      ],
      "text/plain": [
       "X[0, 1]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "refine_matrixelement(X[1, 0], Q.symmetric(X))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 方法 sympy.assumptions.refine.refine_re(expr, assumptions)\n",
    "\n",
    "实部的精炼程序。\n",
    "\n",
    "### 例子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "from sympy.assumptions.refine import refine_re\n",
    "from sympy import Q, re\n",
    "from sympy.abc import x\n",
    "print(refine_re(re(x), Q.real(x)))\n",
    "print(refine_re(re(x), Q.imaginary(x)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 方法 sympy.assumptions.refine.refine_sign(expr, assumptions)\n",
    "\n",
    "签名处理程序。\n",
    "\n",
    "### 例子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "-1\n",
      "0\n",
      "I\n",
      "-I\n"
     ]
    }
   ],
   "source": [
    "from sympy.assumptions.refine import refine_sign\n",
    "from sympy import Symbol, Q, sign, im\n",
    "x = Symbol('x', real = True)\n",
    "expr = sign(x)\n",
    "print(refine_sign(expr, Q.positive(x) & Q.nonzero(x)))\n",
    "print(refine_sign(expr, Q.negative(x) & Q.nonzero(x)))\n",
    "print(refine_sign(expr, Q.zero(x)))\n",
    "y = Symbol('y', imaginary = True)\n",
    "expr = sign(y)\n",
    "print(refine_sign(expr, Q.positive(im(y))))\n",
    "print(refine_sign(expr, Q.negative(im(y))))"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "40d3a090f54c6569ab1632332b64b2c03c39dcf918b08424e98f38b5ae0af88f"
  },
  "kernelspec": {
   "display_name": "Python 3.8.8 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
